//package com.yvon.maple.cloud.web.support;
//
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.json.JSONObject;
//import cn.hutool.json.JSONUtil;
//import com.yvon.boot.redis.service.RedisService;
//import com.yvon.maple.utils.ApplicationContextUtils;
//import com.yvon.maple.utils.RsaUtils;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.IOUtils;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpInputMessage;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.util.Assert;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
//
//import java.io.ByteArrayInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.lang.reflect.Type;
//import java.nio.charset.StandardCharsets;
//
///**
// * 请求参数加密请求
// *
// * @author : Yvon
// * @since : 2021-11-08
// */
//
////TODO 使用时请放开该注解
//// @RestControllerAdvice(basePackages = "com.aiadver")
//@Slf4j
//public class ParamEncryptRequestBodyAdvice implements RequestBodyAdvice {
//
//	@Override
//	public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
//		return methodParameter.hasParameterAnnotation(RequestBody.class) && (methodParameter.hasMethodAnnotation(ParamDecrypt.class) || methodParameter.hasParameterAnnotation(ParamDecrypt.class));
//	}
//
//	@Override
//	public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
//		return o;
//	}
//
//	@Override
//	public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
//		return new HttpInputMessage() {
//			@SneakyThrows
//			@Override
//			public InputStream getBody() throws IOException {
//				log.info("此处进行解密数据");
//				String body = IOUtils.toString(httpInputMessage.getBody());
//				ParamDecrypt decrypt = methodParameter.getParameterAnnotation(ParamDecrypt.class);
//				String key = decrypt.value();
//				boolean invalid = decrypt.invalid();
//				String content;
//				// 如果key不为空，则为json格式
//				if (StrUtil.isNotEmpty(key)) {
//					JSONObject jsonObject = JSONUtil.parseObj(body);
//					content = jsonObject.get(key).toString();
//				}else {
//					content = body;
//				}
//				Assert.notNull(content, "密文不能为空");
//				byte[] bytes = RsaUtils.decryptByPrivateKey(content).getBytes(StandardCharsets.UTF_8);
//				if (invalid) {
//					RedisService redisService = ApplicationContextUtils.getBean(RedisService.class);
//					Assert.isTrue(!redisService.hasKey(content), "该密文已被使用，请重新提交密文");
//					redisService.set(content, null);
//				}
//				return new ByteArrayInputStream(bytes);
//			}
//
//			@Override
//			public HttpHeaders getHeaders() {
//				return httpInputMessage.getHeaders();
//			}
//		};
//	}
//
//	@Override
//	public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
//		return o;
//	}
//}
